package daos

import (
	"database/sql/driver"
	"time"
)

type Time time.Time

func (t *Time) UnmarshalJSON(data []byte) (err error) {
	now, err := time.ParseInLocation(time.DateTime, string(data), time.Local)
	*t = Time(now)
	return
}

func (t *Time) MarshalJSON() ([]byte, error) {
	b := make([]byte, 0, len(time.DateTime))
	b = append(b, '"')
	b = time.Time(*t).AppendFormat(b, time.DateTime)
	b = append(b, '"')
	return b, nil
}

func (t *Time) String() string {
	return time.Time(*t).Format(time.DateTime)
}

func (t *Time) Scan(val interface{}) {
	s := val.(*time.Time)
	*t = Time(*s)
}

func (t *Time) Value() (driver.Value, error) {
	if t == nil {
		return nil, nil
	}

	val := time.Time(*t)
	return val, nil
}

func (t *Time) Format() string {
	return time.Time(*t).Format(time.DateTime)
}
